package com.a008cjx.myrecyclerview508cjx.model;

import com.a008cjx.myrecyclerview508cjx.bean.ArticalBean;
import com.a008cjx.myrecyclerview508cjx.iface.ArticalListener;
import com.a008cjx.myrecyclerview508cjx.iface.Articaliface;
import com.a008cjx.myrecyclerview508cjx.service.ArticalService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/16.
 */

public class ArticalModel implements Articaliface {
    private Retrofit retrofit;
    private String BASEURL = "http://amicool.neusoft.edu.cn/";

    public ArticalModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticalListener listener) {
        ArticalService articalService=retrofit.create(ArticalService.class);
        Call<List<ArticalBean>> call=articalService.getArticleList(mod,page,sessionID);
        call.enqueue(new Callback<List<ArticalBean>>() {
            @Override
            public void onResponse(Call<List<ArticalBean>> call, Response<List<ArticalBean>> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("Fall");
                }
            }

            @Override
            public void onFailure(Call<List<ArticalBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
